/**
 * Copyright (c) 2015-2018, Golly Hu 胡高 (3722711@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hg.jfsa.manage.service;

import cn.hg.jfsa.model.JfsaDict;
import cn.hg.jfsa.model.JfsaFile;
import com.jfinal.aop.Inject;
import com.jfinal.kit.FileKit;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Golly
 *
 */
public class JfsaFileService {

	private static String FILE_TYPE = "file_type";

	@Inject
	private JfsaDictService dictService;

	/**
	 * 文件类型：file：一般文件、image：图片、video：视频、audio：音频、zip：压缩包、doc：文档、folder：文件夹
	 */
	public static String[] imageExtsNames = {"jpg", "gif", "bmp", "png", "jpeg"};
	public static String[] videoExtsNames = {"mp4", "mov"};
	public static String[] audioExtsNames = {"mp3", "mav"};
	public static String[] zipExtsNames = {"zip", "rar", "7z"};
	public static String[] docExtsNames = {"doc", "docx", "txt", "wps", "rtf", "pdf", ""};

	public static Long byteK = 1024L;

	public static Long byte10K = 10 * byteK;

	public static Long byteM = byteK * byteK;

	public static DecimalFormat df = new DecimalFormat("0.0");

	public String getFileType(String fileName) {
		String ext = FileKit.getFileExtension(fileName);
		String type = "file";
		List<JfsaDict> dictList = dictService.getByParentKey(FILE_TYPE, "on");
		for (JfsaDict dict : dictList) {
			String[] typeArray = dict.getValue().split(",");
			if (this.inArray(typeArray, ext)) {
				type = dict.getKey();
				break;
			}
		}
		return type;
	}

	private boolean inArray(String[] arrs, String val) {
		for (String arr : arrs) {
			if (arr.equalsIgnoreCase(val)) {
				return true;
			}
		}
		return false;
	}

	public List<JfsaFile> getFiles(File path) {
		List<JfsaFile> fileObjectList = new ArrayList<>();

		File[] files = path.listFiles();
		for (File f : files) {
			if (f.isDirectory()) {
				JfsaFile fo = new JfsaFile();
				fo.setType("folder");
				fo.setName(f.getName());
				fo.setFolder("");
				fo.setSize(String.valueOf(f.listFiles().length));

				fileObjectList.add(fo);
			}
		}
		for (File f : files) {
			if (!f.isDirectory()) {
				JfsaFile fo = new JfsaFile();
				fo.setType(getFileType(f.getName()));
				fo.setSize(getSizeName(f.length()));
				fo.setName(f.getName());
				fo.setExtName(FileKit.getFileExtension(f.getName()));
				fo.setFolder("");

				fileObjectList.add(fo);
			}
		}
		return fileObjectList;
	}

	public List<JfsaFile> getFile(String path) {
		return getFiles(new File(path));
	}

	public String getSizeName(Long size) {
		if (size < byte10K) {
			return String.valueOf(size) + " B";
		} else if (size > byte10K && size < byteM) {
			return df.format(size / (double) byteK) + " K";
		} else {
			return df.format(size / (double) byteM) + " M";
		}
	}
}
